/**
 * 
 */
package org.mspring.mlog.web.module.stat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.mspring.mlog.web.module.stat.factory.AbstractStatCmd;
import org.mspring.mlog.web.module.stat.factory.StatCmdFactory;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Gao Youbo
 * @since 2012-11-16
 * @Description
 * @TODO 提供统计的Controller
 */
@Widget
@RequestMapping("/stat")
public class StatScriptWidget extends AbstractStatWidget {
    private static final Logger log = Logger.getLogger(StatScriptWidget.class);

    /**
     * 该方法用于记录统计信息
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/set")
    @ResponseBody
    public ResponseEntity set(HttpServletRequest request, HttpServletResponse response) {
        ResponseEntity rsp = new ResponseEntity();
        if (!validateRequest(request)) {
            log.warn("can't validate request!");
            rsp.setSuccess(false);
            return rsp;
        }
        String cmd = RequestUtils.getRequestParameterAsString(request, "cmd");
        if (StringUtils.isBlank(cmd)) {
            rsp.setSuccess(false);
            rsp.setMessage("cmd 参数不能为空");
            return rsp;
        }
        AbstractStatCmd cmdMethod = StatCmdFactory.getCmd(cmd);
        boolean flag = cmdMethod.execute(request);
        rsp.setSuccess(flag);
        return rsp;
    }
}
